package mya_dc.client.gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableColumn;

import mya_dc.client.core.ClientSettings;
import mya_dc.shared_classes.CompilationErrors;
import mya_dc.shared_classes.MyaCompilationError;


/**
 * A special window for errors.
 *
 * @author      Adam Levi
 * <br>			MYA
 */
public class ErrorsWindow extends JFrame
{
	private static final long serialVersionUID = -5687094496920585260L;
	private JTable errorsTable;
	private JScrollPane scrollPane;
	/**
	 * Create the dialog
	 */
	public ErrorsWindow() {
		super();
		setTitle("Errors Window");
		setMinimumSize(new Dimension(800, 600));
		setSize(new Dimension(800, 600));
		getContentPane().setLayout(null);
		setBounds(100, 100, 500, 375);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "Errors And Warnings", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, SystemColor.textHighlight));
		panel.setBounds(10, 10, 774, 548);
		getContentPane().add(panel);

		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 22, 754, 516);
		panel.add(scrollPane);

		errorsTable = new JTable();
		errorsTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				doDoubleClickOnTable(errorsTable.rowAtPoint(e.getPoint()));
			}
		});
		errorsTable.setFont(new Font("", Font.PLAIN, 14));
		scrollPane.setViewportView(errorsTable);
		//
	}

	private ClientSettings m_Settings;
	private CompilationErrors m_Errors;
	
	
	/**
	 * Updates the table's columns' width
	 */
	private void updateColumnWidth()
	{
		errorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumn col = null;
		col = errorsTable.getColumnModel().getColumn(0);
		col.setPreferredWidth(40);
		col = errorsTable.getColumnModel().getColumn(1);
		col.setPreferredWidth(150);
		col = errorsTable.getColumnModel().getColumn(2);
		col.setPreferredWidth(40);
		col = errorsTable.getColumnModel().getColumn(3);
		col.setPreferredWidth(scrollPane.getWidth() - (230 + 10));
	}
	
	
	/**
	 * a method that opens the given error in an editor (if possible)
	 * 
	 * @param rowIndex - the row number that was clicked
	 */
	private void doDoubleClickOnTable(int rowIndex)
	{
		if (rowIndex >= errorsTable.getRowCount())
			return;
		Object[] arr = m_Errors.toArray();
		MyaCompilationError error = (MyaCompilationError)arr[rowIndex];
		if (error.m_strFileName == null || error.m_strFileName.equals(""))
			return;
		int line = -1;
		try
		{
			line = Integer.parseInt(error.m_strLineNum);
		}
		catch(NumberFormatException e) { }
		ClientMainWindow.openFileOnEditor(
				error.m_strFileName,
				line,
				this,
				m_Settings.editor,
				m_Settings.editorPath);
	}
	
	/**
	 * updates the window
	 * 
	 * @param errors - compilation errors
	 * @param settings - the client's settings
	 */
	public void doUpdateTable(
			CompilationErrors errors,
			ClientSettings settings)
	{
		m_Settings = settings;
		m_Errors = errors;
		errorsTable.setModel(errors);
		errorsTable.updateUI();
		updateColumnWidth();
	}
	
}
